package com.mondo.financialwallet.data.net;

import com.mondo.financialwallet.BuildConfig;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class DefaultWebService {

    private static DefaultWebApi mInstance;

    public static DefaultWebApi get() {
        if (mInstance == null) {
            synchronized (DefaultWebService.class) {
                if (mInstance == null) {
                    new DefaultWebService();
                }
            }
        }

        return mInstance;
    }

    private DefaultWebService() {
        Retrofit mRetrofit = new Retrofit.Builder()
                .client(okhttpclient())
                .baseUrl(BuildConfig.DEFAULT_DOMAIN)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        mInstance = mRetrofit.create(DefaultWebApi.class);
    }

    private OkHttpClient okhttpclient() {
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(new HeaderInterceptor())
                .addNetworkInterceptor(logInterceptor)
                .build();
    }
}
